package com.xinli.business.service.impl;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinli.business.domain.FtMember;
import com.xinli.common.utils.DateUtils;
import com.xinli.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xinli.business.mapper.ConsultantApplicationMapper;
import com.xinli.business.domain.ConsultantApplication;
import com.xinli.business.service.IConsultantApplicationService;

/**
 * 咨询师入驻申请Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-28
 */
@Service
public class ConsultantApplicationServiceImpl extends ServiceImpl<ConsultantApplicationMapper,ConsultantApplication>  implements IConsultantApplicationService
{
    @Autowired
    private ConsultantApplicationMapper consultantApplicationMapper;

    /**
     * 查询咨询师入驻申请
     *
     * @param id 咨询师入驻申请主键
     * @return 咨询师入驻申请
     */
    @Override
    public ConsultantApplication selectConsultantApplicationById(Long id)
    {
        return consultantApplicationMapper.selectConsultantApplicationById(id);
    }

    /**
     * 查询咨询师入驻申请列表
     *
     * @param consultantApplication 咨询师入驻申请
     * @return 咨询师入驻申请
     */
    @Override
    public List<ConsultantApplication> selectConsultantApplicationList(ConsultantApplication consultantApplication)
    {
        return consultantApplicationMapper.selectConsultantApplicationList(consultantApplication);
    }

    /**
     * 新增咨询师入驻申请
     *
     * @param consultantApplication 咨询师入驻申请
     * @return 结果
     */
    @Override
    public int insertConsultantApplication(ConsultantApplication consultantApplication)
    {
            return consultantApplicationMapper.insertConsultantApplication(consultantApplication);
    }

    /**
     * 修改咨询师入驻申请
     *
     * @param consultantApplication 咨询师入驻申请
     * @return 结果
     */
    @Override
    public int updateConsultantApplication(ConsultantApplication consultantApplication)
    {
        return consultantApplicationMapper.updateConsultantApplication(consultantApplication);
    }

    /**
     * 批量删除咨询师入驻申请
     *
     * @param ids 需要删除的咨询师入驻申请主键
     * @return 结果
     */
    @Override
    public int deleteConsultantApplicationByIds(Long[] ids)
    {
        return consultantApplicationMapper.deleteConsultantApplicationByIds(ids);
    }

    /**
     * 删除咨询师入驻申请信息
     *
     * @param id 咨询师入驻申请主键
     * @return 结果
     */
    @Override
    public int deleteConsultantApplicationById(Long id)
    {
        return consultantApplicationMapper.deleteConsultantApplicationById(id);
    }


    @Override
    public  int  getYearsOfExperience(Long memberId) {
        // 查询咨询经历
        QueryWrapper<ConsultantApplication> queryWrapperget = new QueryWrapper<>();
        queryWrapperget.eq("status",1);
        queryWrapperget.eq("content_type","8");
        queryWrapperget.eq("user_id",memberId);
        List<ConsultantApplication> list = this.list(queryWrapperget);
        // 计算总从业月份数
        long totalMonths = 0;
        for (ConsultantApplication application : list) {
            LocalDate startDate = DateUtils.dateToLocalDate(application.getConsultationStartDate8());
            LocalDate endDate = DateUtils.dateToLocalDate(application.getConsultationEndDate8());

            if (startDate != null && endDate != null && !endDate.isBefore(startDate)) {
                // 计算两个日期之间的月份数
                long monthsBetween = ChronoUnit.MONTHS.between(startDate, endDate);
                totalMonths += monthsBetween;
            }
        }
        // 转换为年数（取整）
        int totalYears = (int) (totalMonths / 12);
        // 可选：返回带小数的年数，例如 3.5 年
        double totalYearsWithDecimal = (double) totalMonths / 12;
        return totalYears;


    }



    @Override
    public void setConsultantTime(FtMember member){
        QueryWrapper<ConsultantApplication> consultantApplicationQueryWrapper =new QueryWrapper<>();
        consultantApplicationQueryWrapper.eq("user_id",member.getMemberId());
        consultantApplicationQueryWrapper.eq("status",1);
        long count = this.count(consultantApplicationQueryWrapper);
        if(count ==0) {
            // 设置入驻时间
            member.setConsultantTime(new Date());
        }

    }

    @Override
    public void updateMeneberInfo(ConsultantApplication  vo,  FtMember member){
        String contentType = vo.getContentType();

        Long status = vo.getStatus();
        if(1  == status  ){
            if("1".equals(contentType)){
                // 职业照片和视频
                member.setProfilePhotoUrl(vo.getProfessionalPhoto1());
            }
            if("2".equals(contentType)){
                // 身份认证
                String documentType2 = vo.getDocumentType2();
                //身份认证 - 真实姓名
                String realName2 = vo.getRealName2();
                //身份认证 - 证件号码
                String documentNumber2 = vo.getDocumentNumber2();
                //身份认证 - 证件照片
                String documentPhotos2 = vo.getDocumentPhotos2();
                member.setName(realName2);
                member.setIdCard(documentNumber2);
            }else if("3".equals(contentType)){
                //证书名称
                String certificateName3 = vo.getCertificateName3();
                //证书编号
                String certificateNumber3 = vo.getCertificateNumber3();
                //证书发证日期
                Date certificateIssuanceDate3 = vo.getCertificateIssuanceDate3();
                // 证书照片
                String certificatePhotos3 = vo.getCertificatePhotos3();

            }else  if("4".equals(contentType)){
                // 擅长领域
                String expertiseField4 = vo.getExpertiseField4();
                member.setExpertiseDemographics(expertiseField4);
            }else if("5".equals(contentType)){
                // 擅长群体
                String expertiseGroup5 = vo.getExpertiseGroup5();
                member.setExpertiseTopics(expertiseGroup5);
            }else if("6".equals(contentType)){
                // 流派取向
                String orientation6 = vo.getOrientation6();
                member.setExpertiseGenres(orientation6);
            }else if("7".equals(contentType)){
                // 培训经历
                String trainingOrganization7 = vo.getTrainingOrganization7();
                String trainingProject7 = vo.getTrainingProject7();
                //培训时长
                Long trainingDuration7 = vo.getTrainingDuration7();
                String trainingCertificateName7 = vo.getTrainingCertificateName7();
                String trainingCertificateNumber7 = vo.getTrainingCertificateNumber7();
                Date trainingCertificationDate7 = vo.getTrainingCertificationDate7();
                String trainingPhotos7 = vo.getTrainingPhotos7();
                if(!StringUtils.isEmpty(member.getTrainingDuration())){

                    double add = NumberUtil.add(Double.valueOf(member.getTrainingDuration()), Double.valueOf(trainingDuration7));
                    member.setTrainingDuration(add+"");
                }else{
                    member.setTrainingDuration(trainingDuration7+"");
                }
                /*  member.setTrainingExperience(trainingProject7);*/
            }else  if("8".equals(contentType)){
                String consultationOrganization8 = vo.getConsultationOrganization8();
            }else if("9".equals(contentType)){
                //督导时长
                Long supervisionDuration9 = vo.getSupervisionDuration9();
                if(!StringUtils.isEmpty(member.getSupervisionDuration())){
                    double add = NumberUtil.add(Double.valueOf(member.getSupervisionDuration()), Double.valueOf(supervisionDuration9));
                    member.setSupervisionDuration(add+"");
                }else {
                    member.setSupervisionDuration(supervisionDuration9+"");
                }
            }
        }

    }




}
